<template>
  <a-card :bordered="false" class="card-area">
    <!-- 搜索区域 -->
    <!-- <a-form :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }" layout="horizontal">
      <a-row>
        <a-col :md="6" :sm="24">
          <a-form-item label="审核状态">
            <a-select
              :allowClear="true"
              :dropdownStyle="{ maxHeight: '220px', overflow: 'auto' }"
              v-model="queryParams.status">
              <a-select-option :value="1">未审核</a-select-option>
              <a-select-option :value="2">通过</a-select-option>
              <a-select-option :value="3">不通过</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item label="类型">
            <a-select
              :allowClear="true"
              :dropdownStyle="{ maxHeight: '220px', overflow: 'auto' }"
              v-model="queryParams.type">
              <a-select-option :value="1">充值</a-select-option>
              <a-select-option :value="2">提现</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <span style="float: right; margin-top: 3px;">
            <a-button type="primary" @click="search">查询</a-button>
            <a-button style="margin-left: 8px" @click="reset">重置</a-button>
          </span>
        </a-col>
      </a-row>
    </a-form> -->
    <div>
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
         :columns="columns"
         :dataSource="dataSource"
         :pagination="pagination"
         :loading="loading"
         size="small"
         @change="handleTableChange">
        <template slot="email" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div>{{text}}</div>
            </template>
            <p style="width: 150px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>
        <template slot="operation" slot-scope="text, record">
          <a-icon
            type="setting"
            theme="twoTone"
            twoToneColor="#4a9ff5"
            @click="edit(record)"
            title="审核">
          </a-icon>
        </template>
      </a-table>
    </div>

    <a-modal
      :visible="visible"
      width="800px"
      title="审核"
      ok-text="确认"
      cancel-text="取消"
      @cancel="handleCancel"
      @ok="confirmModal"
    >
      <a-form :form="form">
        <a-form-item label='付款户名' v-bind="formItemLayout">
          {{records.fukuanhuming}}
        </a-form-item>
        <a-form-item label='付款银行' v-bind="formItemLayout">
          {{records.fukuanyinhang}}
        </a-form-item>
        <a-form-item label='付款账户' v-bind="formItemLayout">
          {{records.fukuanzhanghu}}
        </a-form-item>
        <a-form-item label='金额' v-bind="formItemLayout">
          {{records.balance}}
        </a-form-item>
        <a-form-item label='图片' v-bind="formItemLayout">
          <img style="display: block;max-width: 100%;max-height: 400px" :src="`/auction/images${records.tupian}`" alt="">
        </a-form-item>
        <a-form-item label='审核状态' v-bind="formItemLayout">
          <a-select v-decorator="['status']">
            <a-select-option :value="1">未审核</a-select-option>
            <a-select-option :value="2">通过</a-select-option>
            <a-select-option :value="3">不通过</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>

  </a-card>
</template>

<script>
import { vehicleFields } from './config.js'
const formItemLayout = {
  labelCol: { span: 4 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'BalanceRecord',
  components: {},
  data () {
    return {
      formItemLayout,
      form: this.$form.createForm(this),
      visible: false,
      queryParams: {},
      filteredInfo: null,
      sortedInfo: null,
      paginationInfo: null,
      dataSource: [],
      selectedRowKeys: [],
      loading: false,
      records: {},
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      }
    }
  },
  computed: {
    columns () {
      return vehicleFields
    }
  },
  mounted () {
    this.fetch()
  },
  methods: {
    handleCancel () {
      this.visible = false
    },
    edit (record) {
      this.recordId = record.id
      this.records = record
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue({status: record.status * 1})
      })
    },
    confirmModal () {
      this.$post('balancerecord/updateUserBalance', {
        id: this.recordId,
        status: this.form.getFieldValue('status')
      }).then(({ data }) => {
        this.visible = false
        this.$message.success(data.message)
        this.search()
      }).catch(() => {
      })
    },
    search () {
      let {sortedInfo, filteredInfo} = this
      let sortField, sortOrder
      // 获取当前列的排序和列的过滤规则
      if (sortedInfo) {
        sortField = sortedInfo.field
        sortOrder = sortedInfo.order
      }
      this.fetch({
        sortField: sortField,
        sortOrder: sortOrder,
        ...this.queryParams,
        ...filteredInfo
      })
    },
    reset () {
      // 取消选中
      this.selectedRowKeys = []
      // 重置分页
      this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
      if (this.paginationInfo) {
        this.paginationInfo.current = this.pagination.defaultCurrent
        this.paginationInfo.pageSize = this.pagination.defaultPageSize
      }
      // 重置列过滤器规则
      this.filteredInfo = null
      // 重置列排序规则
      this.sortedInfo = null
      // 重置查询参数
      this.queryParams = {}
      this.fetch()
    },
    handleTableChange (pagination, filters, sorter) {
      // 将这三个参数赋值给Vue data，用于后续使用
      this.paginationInfo = pagination
      this.filteredInfo = filters
      this.sortedInfo = sorter

      this.fetch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...this.queryParams,
        ...filters
      })
    },
    fetch (params = {}) {
      // 显示loading
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        params.pageNum = this.paginationInfo.current
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }

      // 删除掉undefined,以免接口报错
      Object.keys(params).forEach(k => {
        if (params[k] === undefined) {
          delete params[k]
        }
      })
      this.$get('balancerecord/pageBalanceRecord', {
        ...params
      }).then((r) => {
        let data = r.data
        const pagination = { ...this.pagination }
        pagination.total = data.total
        this.dataSource = data.rows
        this.pagination = pagination
        // 数据加载完毕，关闭loading
        this.loading = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
@import "../../../../static/less/Common";
</style>
